/*  SSMatrixPropertiesPanel.m
 *  MESLib
 * 
 * Copyright 2003-2009 Starline Software Ltd
 * 
 * This file is part of MESLib.
 * 
 * MESLib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MESLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with MESLib. If not, see <http://www.gnu.org/licenses/>.
 */
//  Created by Tobias Haughton on Thu Jul 24 2003.
//

#import "SSMatrixPropertiesPanel.h"
#import "SSCatalystDocument.h"

@implementation SSMatrixPropertiesPanel

+ (id)sharedMatrixPropertiesPanelController {
   static SSMatrixPropertiesPanel *sharedMatrixPropertiesPanelController = nil;

   if(!sharedMatrixPropertiesPanelController) {
      sharedMatrixPropertiesPanelController = [[SSMatrixPropertiesPanel allocWithZone:[self zone]] init];
   }
   return sharedMatrixPropertiesPanelController;
}

- (id)init {
   self = [super initWithWindowNibName:@"MatrixPropertiesPanel"];
   if (self) {
      [self setWindowFrameAutosaveName:@"MatrixPropertiesPanel"];
   }
   return self;
}

- (void)awakeFromNib
{
   //set the colors
   [whiteColorWell setColor:SSWhiteColor];
   [yellowColorWell setColor:SSYellowColor];
   [greenColorWell setColor:SSGreenColor];
   [lightBlueColorWell setColor:SSLightBlueColor];
   [blueColorWell setColor:SSBlueColor];
   [purpleColorWell setColor:SSPurpleColor];
   [orangeColorWell setColor:SSOrangeColor];
   [redColorWell setColor:SSRedColor];
   
   //set the other control values
   [self setControlValues];
}

- (void)setMainWindow:(NSWindow *)mainWindow {
   needsUpdate = YES;
}

- (void)windowDidUpdate:(NSNotification *)notification {
   //this is called each time the window updates
   if(needsUpdate) {
      [self setControlValues];
      needsUpdate = NO;
   }
}

- (void)setControlValues
{
   if([[[NSDocumentController sharedDocumentController] currentDocument] whiteColorDesignation]) {
      [whiteColorField setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] whiteColorDesignation]];
   } else {
      [whiteColorField setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] yellowColorDesignation]) {
      [yellowColorField setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] yellowColorDesignation]];
   } else {
      [yellowColorField setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] greenColorDesignation]) {
      [greenColorField setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] greenColorDesignation]];
   } else {
      [greenColorField setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] lightBlueColorDesignation]) {
      [lightBlueColorField setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] lightBlueColorDesignation]];
   } else {
      [lightBlueColorField setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] blueColorDesignation]) {
      [blueColorField setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] blueColorDesignation]];
   } else {
      [blueColorField setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] purpleColorDesignation]) {
      [purpleColorField setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] purpleColorDesignation]];
   } else {
      [purpleColorField setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] orangeColorDesignation]) {
      [orangeColorField setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] orangeColorDesignation]];
   } else {
      [orangeColorField setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] redColorDesignation]) {
      [redColorField setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] redColorDesignation]];
   } else {
      [redColorField setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] title]) {
      [titleField setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] title]];
   } else {
      [titleField setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] author]) {
      [authorField setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] author]];
   } else {
      [authorField setStringValue:[NSString string]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] chartDate]) {
      [dateField setObjectValue:[[[NSDocumentController sharedDocumentController] currentDocument] chartDate]];
   } else {
      [dateField setObjectValue:[NSCalendarDate calendarDate]];
   }

   if([[[NSDocumentController sharedDocumentController] currentDocument] projectFolder]) {
      [projectFolderField setStringValue:[[[NSDocumentController sharedDocumentController] currentDocument] projectFolder]];
   } else {
      [projectFolderField setStringValue:@"~/Documents"];
   } 
}

- (IBAction)saveDefaults:(id)sender
{
   //save all of the values to the user defaults 
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   
   [defaults setObject:[whiteColorField stringValue] forKey:SSDefaultWhiteLabelDesignationKey];
   [defaults setObject:[yellowColorField stringValue] forKey:SSDefaultYellowLabelDesignationKey];
   [defaults setObject:[greenColorField stringValue] forKey:SSDefaultGreenLabelDesignationKey];
   [defaults setObject:[lightBlueColorField stringValue] forKey:SSDefaultLightBlueLabelDesignationKey];
   [defaults setObject:[blueColorField stringValue] forKey:SSDefaultBlueLabelDesignationKey];
   [defaults setObject:[purpleColorField stringValue] forKey:SSDefaultPurpleLabelDesignationKey];
   [defaults setObject:[orangeColorField stringValue] forKey:SSDefaultOrangeLabelDesignationKey];
   [defaults setObject:[redColorField stringValue] forKey:SSDefaultRedLabelDesignationKey];
   [defaults setObject:[titleField stringValue] forKey:SSDefaultTitleKey];
   [defaults setObject:[authorField stringValue] forKey:SSDefaultAuthorKey];
   [defaults setObject:[dateField objectValue] forKey:SSDefaultDateKey];
   [defaults setObject:[projectFolderField stringValue] forKey:SSDefaultProjectFolderKey];
}

- (IBAction)chooseProjectFolder:(id)sender
{
   NSOpenPanel *openPanel = [NSOpenPanel openPanel];
   int result;
   NSString *path;
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

   [openPanel setTitle:@"Select project folder for new file"];
   [openPanel setPrompt:@"Select"];
   [openPanel setAllowsMultipleSelection:NO];
   [openPanel setCanChooseDirectories:YES];
   [openPanel setCanChooseFiles:NO];

   result = [openPanel runModalForDirectory:[defaults objectForKey:SSDefaultProjectFolderKey] file:@""];

   if(result == NSOKButton) {
      //user has selected a folder so set the value in the document and field
      path = [openPanel filename];
      [[[NSDocumentController sharedDocumentController] currentDocument] setProjectFolder:path];
      [projectFolderField setStringValue:path];
   }
}

- (void)controlTextDidEndEditing:(NSNotification *)note
{
   //called when a text field stops editing
   switch([[note object] tag]) {
      case 0:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] whiteColorDesignation]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setWhiteColorDesignation:[[note object] stringValue]];
         }
         break;
      case 1:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] yellowColorDesignation]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setYellowColorDesignation:[[note object] stringValue]];
         }
         break;
      case 2:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] greenColorDesignation]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setGreenColorDesignation:[[note object] stringValue]];
         }
         break;
      case 3:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] lightBlueColorDesignation]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setLightBlueColorDesignation:[[note object] stringValue]];
         }
         break;
      case 4:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] blueColorDesignation]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setBlueColorDesignation:[[note object] stringValue]];
         }
         break;
      case 5:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] purpleColorDesignation]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setPurpleColorDesignation:[[note object] stringValue]];
         }
         break;
      case 6:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] orangeColorDesignation]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setOrangeColorDesignation:[[note object] stringValue]];
         }
         break;
      case 7:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] redColorDesignation]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setRedColorDesignation:[[note object] stringValue]];
         }
      case 8:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] title]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setTitle:[[note object] stringValue]];
         }
         break;
      case 9:
         if(![[[note object] stringValue] isEqualToString:[[[NSDocumentController sharedDocumentController] currentDocument] author]]) { //if it hasn't changed don't set the same value
            [[[NSDocumentController sharedDocumentController] currentDocument] setAuthor:[[note object] stringValue]];
         }
         break;
      case 10:
         [[[NSDocumentController sharedDocumentController] currentDocument] setChartDate:[[note object] objectValue]];
         break;
      default:
         break;
   }
}   

- (void)dealloc
{
   [[NSNotificationCenter defaultCenter] removeObserver:self];
   [super dealloc];
}

@end